package com.ahq.main;

import java.awt.Insets;

import com.dalonedrau.jogl.engine.GameApplet;
import com.dalonedrau.jogl.engine.GlobalConsts;
import com.dalonedrau.jogl.engine.JOGLErrorHandler;
import com.dalonedrau.jogl.engine.Render;
import com.dalonedrau.jogl.opengl.sprite.BasicSpriteObject;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObject;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObjectFactory;
import com.dalonedrau.jogl.opengl.ui.GuiTextItem;
import com.dalonedrau.jogl.opengl.ui.border.GuiBorder;

/**
 * A simple border, similar to those found in the early 90's SNES games.
 * @author Donald
 */
public final class DWHPBorder extends GuiBorder {
	/** the border group. */
	private BasicSpriteObject	border;
	/** the parent component. */
	private GuiTextItem			parent;
	/** 
	 * Creates a new instance of {@link DWHPBorder}.
	 * @param p the parent component
	 */
	public DWHPBorder(final GuiTextItem p) {
		parent = p;
		try {
			SpriteImageObject imgBL =
				SpriteImageObjectFactory.getInstance().getImageByName(
						"dw_hp_frame");
			border = new BasicSpriteObject();
			border.setImageRefId(imgBL.getRefId());
			border.setWidth(imgBL.getWidth());
			border.setHeight(imgBL.getHeight());
			imgBL = null;
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public Insets getInsets() {
		int offset = 3;
		offset += (72 - (parent.getText().length() * 9)) / 2;
		super.setInsets(2, offset, 1, GlobalConsts.INT_003);
		return super.getInsets();
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		Render.getInstance().registerEntity(border);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPosition(final double x, final double y, final double z) {
		super.setPosition(x, y, z);
		border.setPosition(x, y, z);
		border.clearFloatStore();
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPreferredSize(final int width, final int height) {
		super.setPreferredSize(width, height);
		if (super.getPosition() != null) {
			border.setPosition(super.getPosition());
		}
	}
}
